import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {UrlService} from '../common/service/url.service';
import {RegionPolicyCondition} from '../content/model/region-policy-condition';
import {Observable} from 'rxjs';
import {ApiRes} from '../common/model/response';
import {map} from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class PolicyAnalysisChartService {

  constructor(private http: HttpClient,
              private urlService: UrlService) {
  }

  policyCountWithYear(): Observable<ApiRes> {
    const url = this.urlService.getWebUrl('/policyAnalysis/countPolicyWithPublishYear');
    return this.http.get<ApiRes>(url).pipe(map(res => {
      // 因为echarts对数据源的格式要求，在此将数据源格式化
      const data = [];
      for (let i = 0; i < res.result[0].chartCounts.length; i++) {
        const rowTemp = [];
        for (const year of res.result) {
          rowTemp.push(year.chartCounts[i]);
        }
        data.push(rowTemp);
      }
      res.result[0].data = data;
      return res;
    }));

  }
}
